home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1993 July / InfoMagic USENET CD-ROM July 1993.ISO / sources / misc / volume5 / secure < prev    next >
Encoding:
Internet Message Format  |  1989-02-03  |  4.4 KB

  1. Path: xanth!nic.MR.NET!hal!ncoast!allbery
  2. From: dg@lakart.UUCP (David Goodenough)
  3. Newsgroups: comp.sources.misc
  4. Subject: v05i063: Solving the setuid script problem
  5. Message-ID: <8811231522.AA14884@lakart.UUCP>
  6. Date: 3 Dec 88 00:27:57 GMT
  7. Sender: allbery@ncoast.UUCP
  8. Reply-To: dg@lakart.UUCP (David Goodenough)
  9. Lines: 139
  10. Approved: allbery@ncoast.UUCP
  11.  
  12. Posting-number: Volume 5, Issue 63
  13. Submitted-by: "David Goodenough" <dg@lakart.UUCP>
  14. Archive-name: secure
  15.  
  16. [Any setuid gurus want to say whether this is actually secure or not? ++bsa]
  17.  
  18. After all the discussion about the lack of security of setuid shell scripts,
  19. I put the following together. It runs shell scripts, but is a little more
  20. careful as to what it will run. There's no makefile - just compile this,
  21. copy it to /bin/secure, chown it to root, and chmod it 4755. secure.8
  22. describes the layout of /etc/secure - it's just a line for each safe shell
  23. script, containing the full pathname first, and the numeric uid the run the
  24. script.
  25. -- 
  26.     dg@lakart.UUCP - David Goodenough        +---+
  27.                             | +-+-+
  28.     ....... !harvard!xait!lakart!dg            +-+-+ |
  29. AKA:    dg%lakart.uucp@harvard.harvard.edu            +---+
  30.  
  31. --- cut here --- cut here --- cut here --- cut here --- cut here ---
  32. #! /bin/sh
  33. # This is a shell archive, meaning:
  34. # 1. Remove everything above the '#! /bin/sh' line.
  35. # 2. Save the resulting text in a file.
  36. # 3. Execute the file with /bin/sh (NOT csh) to create the files:
  37. #    A: secure.c
  38. #    B: secure.8
  39. #
  40. # Unwrapping with no options prevents overwriting files that already exist;
  41. # giving a '-c' switch causes files to be overwritten regardless.
  42. #
  43. # wrapped by dg@lakart(David Goodenough) on Wed Nov 23 10:13:18 EST 1988
  44. #
  45. if test -f secure.c -a x$1 != x-c
  46. then
  47.     echo shar: file secure.c already exists
  48. else
  49.     echo shar: extracting secure.c
  50. sed "s/^X//" <<\##__EOF..secure.c__## >secure.c
  51. X#include    <stdio.h>
  52. X
  53. X#define        SECURE        "/etc/secure"
  54. X
  55. Xmain(n, a)
  56. Xchar **a;
  57. X {
  58. X    int uid;
  59. X    FILE *fp;
  60. X    char program[100];
  61. X
  62. X    if (strcmp(a[0], "SEC-URE"))/* I'm damned if I know why this is */
  63. X     {                /* necessary, but it is */
  64. X    (void) strcpy(program, a[0]);
  65. X    a[0] = "SEC-URE";
  66. X    execv(program, a);    /* re exec ourselves so setuid bits work */
  67. X    exit(1);        /* this should never happen */
  68. X     }
  69. X    if ((fp = fopen(a[1], "r")) == (FILE *) NULL)
  70. X      exit(1);            /* file not found */
  71. X    (void) fclose(fp);
  72. X    if (a[1][0] != '/')
  73. X      exit(1);            /* only pass an absolute pathname to /bin/sh */
  74. X    if ((fp = fopen(SECURE, "r")) == (FILE *) NULL)
  75. X      exit(1);            /* can't find the file of secure programs */
  76. X    while (fscanf(fp, "%s %d", program, &uid) == 2)
  77. X     {
  78. X    if (strcmp(program, a[1]) == 0)
  79. X     {            /* aha ..... we found our program */
  80. X        (void) fclose(fp);
  81. X        (void) unsetenv("IFS");
  82. X                /* tweak the environment for added safety */
  83. X        (void) setenv("PATH", "/bin:/usr/bin", 1);
  84. X        (void) setuid(uid);    /* set the uid */
  85. X        a[0] = "-sh";
  86. X        execv("/bin/sh", a);
  87. X        exit(1);        /* this should never happen */
  88. X     }
  89. X     }
  90. X    exit(1);            /* come here if we didn't find the program */
  91. X }
  92. ##__EOF..secure.c__##
  93. fi
  94. if test -f secure.8 -a x$1 != x-c
  95. then
  96.     echo shar: file secure.8 already exists
  97. else
  98.     echo shar: extracting secure.8
  99. sed "s/^X//" <<\##__EOF..secure.8__## >secure.8
  100. X.\" dg@lakart - David Goodenough Wed Nov 23 09:47:12 EST 1988
  101. X.\"
  102. X.TH SECURE 8 "Nov 23, 1988"
  103. X.UC 4
  104. X.SH NAME
  105. X.B secure
  106. X\- run setuid shell scripts safely
  107. X.SH SYNOPSIS
  108. X.B #! /bin/secure
  109. X.br
  110. X.SH DESCRIPTION
  111. X.B Secure
  112. Xis never normally executed from a shell. Instead it can be used
  113. Xas the interpreter for shell scripts that need to be run setuid someone
  114. Xelse: this is done by making the first line of the script
  115. X.PP
  116. X.ti+5n
  117. X#! /bin/secure
  118. X.PP
  119. Xrather than the usual
  120. X.PP
  121. X.ti+5n
  122. X#! /bin/sh
  123. X.PP
  124. X.B Secure
  125. Xdetermines if a script can be safely run by looking in
  126. X/etc/secure. This is a list of secure shell scripts, and the numeric
  127. Xuser id that the script is to be run with. A typical line from
  128. X/etc/secure might be
  129. X.PP
  130. X.ti+5n
  131. X/usr/script        0
  132. X.PP
  133. Xwhich would mean that /usr/script should be run as root.
  134. X.PP
  135. XBecause
  136. X.B secure
  137. Xwill only
  138. Xexec a pathname beginning with '/', that comes from /etc/secure, it
  139. Xcannot be used to illegally gain root access.
  140. X.PP
  141. XIn addition, for the sake of security,
  142. X.B secure
  143. Xsets the PATH environment variable back to a simple default, and deletes
  144. Xthe IFS environment variable.
  145. X.SH "SEE ALSO"
  146. X.BR sh (1),
  147. ##__EOF..secure.8__##
  148. fi
  149. # end of shell archive
  150. exit 0
  151.